package com.hcl.orm.sample;

import cn.hutool.core.util.StrUtil;
import com.hcl.orm.sample.dao.BusinentrustDAO;
import com.hcl.orm.sample.domain.Businentrust;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * @author hucl
 * @projectName hcl-orm
 * @description: 性能比对
 * @date 2021/8/1713:49
 */
public class PerformanceSample {
    private static final Logger logger = LoggerFactory.getLogger(PerformanceSample.class);
    private final BusinentrustDAO businentrustDAO;
    private final ConfigurableApplicationContext context;
    private final JdbcTemplate jdbcTemplate;

    public PerformanceSample(final ConfigurableApplicationContext context) {
        this.context = context;
        this.jdbcTemplate = context.getBean(JdbcTemplate.class);
        this.businentrustDAO = context.getBean(BusinentrustDAO.class);
    }

    public void test() {
        long start1 = System.currentTimeMillis();
        for (int i = 0; i < 50; i++) {
            jdbcTemplate.query("select * from prd_busin_entrust where init_date = ?", new Object[]{20210506},
                    new BeanPropertyRowMapper(Businentrust.class));
        }
        long end1 = System.currentTimeMillis();
        logger.info("BeanPropertyRowMapper 方式耗时:" + (end1 - start1) + "ms");

        long start2 = System.currentTimeMillis();
        for (int i = 0; i < 50; i++) {
            jdbcTemplate.query("select * from prd_busin_entrust where init_date = nvl(?,20210506)", ps -> ps.setInt(1, 20210506),
                    (rs, rowNum) -> {
                        Businentrust businentrust = new Businentrust();
                        businentrust.setInitDate(rs.getObject(1, Integer.class));
                        businentrust.setEntrustNo(rs.getObject(2, Long.class));
                        businentrust.setCurrDate(rs.getObject(3, Integer.class));
                        businentrust.setCurrMilltime(rs.getObject(4, Integer.class));
                        businentrust.setOpStation(rs.getObject(5, String.class));
                        businentrust.setOpBranchNo(rs.getObject(6, Integer.class));
                        businentrust.setOperatorNo(rs.getObject(7, String.class));
                        businentrust.setOpEntrustWay(StrUtil.isEmpty(rs.getObject(8, String.class)) ? ' ' : rs.getObject(8, String.class).charAt(0));
                        businentrust.setProdEntrustsrcType(StrUtil.isEmpty(rs.getObject(9, String.class)) ? ' ' : rs.getObject(9, String.class).charAt(0));
                        businentrust.setAllotNo(rs.getObject(10, String.class));
                        businentrust.setBatchNo(rs.getObject(11, Long.class));
                        businentrust.setOriginalAppno(rs.getObject(12, String.class));
                        businentrust.setFundAccount(rs.getObject(13, String.class));
                        businentrust.setClientId(rs.getObject(14, String.class));
                        businentrust.setProdAccount(rs.getObject(15, String.class));
                        businentrust.setTransAccount(rs.getObject(16, String.class));
                        businentrust.setPayAccount(rs.getObject(17, String.class));
                        businentrust.setPayKind(StrUtil.isEmpty(rs.getObject(18, String.class)) ? ' ' : rs.getObject(18, String.class).charAt(0));
                        businentrust.setBankNo(rs.getObject(19, String.class));
                        businentrust.setOppoProdaccount(rs.getObject(20, String.class));
                        businentrust.setOtherTransaccount(rs.getObject(21, String.class));
                        businentrust.setDeveloper(rs.getObject(22, String.class));
                        businentrust.setTaType(StrUtil.isEmpty(rs.getObject(23, String.class)) ? ' ' : rs.getObject(23, String.class).charAt(0));
                        businentrust.setProdtaNo(rs.getObject(24, String.class));
                        businentrust.setProdCode(rs.getObject(25, String.class));
                        businentrust.setProdName(rs.getObject(26, String.class));
                        businentrust.setTradeReportType(StrUtil.isEmpty(rs.getObject(27, String.class)) ? ' ' : rs.getObject(27, String.class).charAt(0));
                        businentrust.setMoneyType(rs.getObject(28, String.class));
                        businentrust.setDividendWay(StrUtil.isEmpty(rs.getObject(29, String.class)) ? ' ' : rs.getObject(29, String.class).charAt(0));
                        businentrust.setChargeType(StrUtil.isEmpty(rs.getObject(30, String.class)) ? ' ' : rs.getObject(30, String.class).charAt(0));
                        businentrust.setProdcodeType(StrUtil.isEmpty(rs.getObject(31, String.class)) ? ' ' : rs.getObject(31, String.class).charAt(0));
                        businentrust.setProdTypeAss(rs.getObject(32, String.class));
                        businentrust.setDiscountRate(rs.getObject(33, Double.class));
                        businentrust.setSwitchProdCode(rs.getObject(34, String.class));
                        businentrust.setSwitchChargeType(StrUtil.isEmpty(rs.getObject(35, String.class)) ? ' ' : rs.getObject(35, String.class).charAt(0));
                        businentrust.setEntrustBalance(rs.getObject(36, Double.class));
                        businentrust.setEntrustPrice(rs.getObject(37, Double.class));
                        businentrust.setInvpayStatus(StrUtil.isEmpty(rs.getObject(38, String.class)) ? ' ' : rs.getObject(38, String.class).charAt(0));
                        businentrust.setProdRevisitStatus(StrUtil.isEmpty(rs.getObject(39, String.class)) ? ' ' : rs.getObject(39, String.class).charAt(0));
                        businentrust.setVerifyStatus(StrUtil.isEmpty(rs.getObject(40, String.class)) ? ' ' : rs.getObject(40, String.class).charAt(0));
//                        businentrust.setSettleDate(rs.getObject(41, Integer.class));
                        businentrust.setBusinessBalance(rs.getObject(43, Double.class));
                        businentrust.setBusinessDate(rs.getObject(44, Integer.class));
                        businentrust.setTaSerialno(rs.getObject(45, String.class));
                        businentrust.setErrorInfo(rs.getObject(46, String.class));
                        businentrust.setErrorCode(rs.getObject(47, String.class));
                        businentrust.setProdriskLevel(rs.getObject(50, Integer.class));
                        businentrust.setCorpRiskLevel(rs.getObject(51, Integer.class));
                        businentrust.setTaRiskLevel(rs.getObject(52, Integer.class));
                        businentrust.setRiskEnddate(rs.getObject(53, Integer.class));
                        businentrust.setProdriskFlag(StrUtil.isEmpty(rs.getObject(52, String.class)) ? ' ' : rs.getObject(52, String.class).charAt(0));
                        businentrust.setEligCheckStr(rs.getObject(53, String.class));
                        businentrust.setCancelSerialNo(rs.getObject(54, String.class));
                        businentrust.setEligConfirmType(StrUtil.isEmpty(rs.getObject(55, String.class)) ? ' ' : rs.getObject(55, String.class).charAt(0));
                        businentrust.setAgreementIdStr(rs.getObject(56, String.class));
                        businentrust.setProdProp(rs.getObject(57, String.class));
                        businentrust.setRedeemType(StrUtil.isEmpty(rs.getObject(58, String.class)) ? ' ' : rs.getObject(58, String.class).charAt(0));
                        businentrust.setRegisterDate(rs.getObject(61, Integer.class));
                        businentrust.setAgencyNo(rs.getObject(60, String.class));
                        businentrust.setOtherAgency(rs.getObject(61, String.class));
                        businentrust.setRatioSx(rs.getObject(62, Double.class));
                        businentrust.setSaleFare(rs.getObject(65, Double.class));
                        businentrust.setOtherFare(rs.getObject(64, Double.class));
                        businentrust.setExceedFlag(StrUtil.isEmpty(rs.getObject(65, String.class)) ? ' ' : rs.getObject(65, String.class).charAt(0));
                        businentrust.setNotradeKind(StrUtil.isEmpty(rs.getObject(66, String.class)) ? ' ' : rs.getObject(66, String.class).charAt(0));
                        businentrust.setIdKind(StrUtil.isEmpty(rs.getObject(67, String.class)) ? ' ' : rs.getObject(67, String.class).charAt(0));
                        businentrust.setIdNo(rs.getObject(68, String.class));
                        businentrust.setOrganFlag(StrUtil.isEmpty(rs.getObject(69, String.class)) ? ' ' : rs.getObject(69, String.class).charAt(0));
                        businentrust.setNetNo(rs.getObject(70, String.class));
                        businentrust.setOtherNetno(rs.getObject(71, String.class));
                        businentrust.setValidDate(rs.getObject(74, Integer.class));
                        businentrust.setDateClear(rs.getObject(73, Integer.class));
                        businentrust.setRemark(rs.getObject(74, String.class));
                        businentrust.setPositionStr(rs.getObject(75, String.class));
                        businentrust.setReportNo(rs.getObject(76, Long.class));
//                        businentrust.setReportDate(rs.getObject(79, String.class));
                        businentrust.setProdcodeSubType(rs.getObject(79, String.class));
                        businentrust.setEligRemark(rs.getObject(80, String.class));
                        businentrust.setReportTime(rs.getObject(81, Integer.class));
                        businentrust.setAfterDiscountRate(rs.getObject(83, Double.class));
                        businentrust.setCurrMilltime(rs.getObject(84, Integer.class));
                        businentrust.setOtcDealFlag(StrUtil.isEmpty(rs.getObject(85, String.class)) ? ' ' : rs.getObject(85, String.class).charAt(0));
                        businentrust.setOtcEntrustStatus(StrUtil.isEmpty(rs.getObject(86, String.class)) ? ' ' : rs.getObject(86, String.class).charAt(0));
                        return businentrust;
                    });
        }
        long end2 = System.currentTimeMillis();
        logger.info("属性赋值方式耗时:" + (end2 - start2) + "ms");

        long start3 = System.currentTimeMillis();
        for (int i = 0; i < 50; i++) {
            businentrustDAO.getBusinentrustRecord(20210506);
        }
        long end3 = System.currentTimeMillis();
        logger.info("simpleMapper 方式耗时:" + (end3 - start3) + "ms");
    }
}
